<?php


namespace app\api\controller;


use app\admin\validate\IDMustBePositiveInt;
use app\api\validate\AddressValidate;
use app\service\model\FreightModel;
use app\service\model\MemberAddressModel;
use app\service\Y;

class Address extends AuthApi
{

    /**
     * 收货地址列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function address_list(): \think\response\Json
    {
        $data = MemberAddressModel::field('id, loc_name, consignee, mobile, lat, lng, distance, address, is_default')
            ->where('member_id', '=', $this->member->id)->select();
        return Y::success($data, '收货地址列表');
    }

    /**
     * 收货地址详情 - 没有指定的获取默认地址 - 没有设置默认地址则返回空
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function address_info(): \think\response\Json
    {
        $id = input('param.id', 0);
        $where[] = ['member_id', '=', $this->member->id];
        if ($id) {
            $where[] = ['id', '=', $id];
        } else {
            $where[] = ['is_default', '=', 1];
        }
        $address = MemberAddressModel::where($where)->find();

        if ($address) {
            // 获取运费
            $address['freight'] = FreightModel::calcFreight($address->distance);
        }
        return Y::success($address, '收货地址详情');
    }

    /**
     * @param AddressValidate $addressValidate
     * @return \think\response\Json
     * @throws \app\service\exception\ParameterException
     * @throws \think\Exception
     */
    public function save_address(AddressValidate $addressValidate)
    {
        $addressValidate->goCheck();
        $param = $addressValidate->getDataByRule();
        $param['member_id'] = $this->member->id;
        $id = input('param.id', 0);

        return MemberAddressModel::saveData($param, $id);
    }

    /**
     * 设置默认收货地址
     * @param IDMustBePositiveInt $IDMustBePositiveInt
     * @return \think\response\Json
     * @throws \app\service\exception\ParameterException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function set_default(IDMustBePositiveInt $IDMustBePositiveInt): \think\response\Json
    {
        $IDMustBePositiveInt->goCheck();
        $id = input('param.id');
        $where = [
            ['member_id', '=', $this->member->id],
            ['id', '=', $id]
        ];

        $address = MemberAddressModel::where($where)->find();
        if (!$address) {
            return Y::error('参数错误');
        }
        if ($address->is_default == 1) {
            return Y::error('请勿重复设置默认地址');
        }
        MemberAddressModel::where('member_id', '=', $this->member->id)->update(['is_default' => 0]);
        $address->save(['is_default' => 1]);
        return Y::success([], '设置成功');
    }

    /**
     * 删除收货地址
     * @param IDMustBePositiveInt $IDMustBePositiveInt
     * @return \think\response\Json
     * @throws \app\service\exception\ParameterException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function del_address(IDMustBePositiveInt $IDMustBePositiveInt): \think\response\Json
    {
        $IDMustBePositiveInt->goCheck();
        $id = input('param.id');
        $where = [
            ['member_id', '=', $this->member->id],
            ['id', '=', $id]
        ];
        $address = MemberAddressModel::where($where)->find();
        if (!$address) {
            return Y::error('参数错误');
        }
        $address->delete();

        return Y::success([], '删除成功');
    }
}